import { Component, OnInit, Input } from '@angular/core';
import { TreeNode, SelectItem } from 'primeng/api';
import { DialogService, ConfirmationService } from 'primeng/primeng';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService } from '@angular-mdl/core';
import { TaskTabService } from './tasktab.service';
import { TaskTabGroupComponent } from './tasktab-group.component';
@Component({
  selector: 'tasklist-config-tab',
  templateUrl: './tasktab.component.html',
  styleUrls: ['./tasktab.component.scss'],
  providers: [TaskTabService, ConfirmationService]
})
export class TaskTabComponent implements OnInit {
  @Input()
  systemUrl: string = '';

  currentGroupId: number;
  selectedNode: TreeNode;//当前选中节点
  loading: boolean = true;
  tabs: TreeNode[];
  tab: any;//当前编辑
  tabForm: FormGroup;

  /** 图标类型 */
  icons: SelectItem[] = new Array(
    { label: 'fa-circle-o', value: 'fa-circle-o' },
    { label: 'fa-cogs', value: 'fa-cogs' },
    { label: 'fa-university', value: 'fa-university' },
    { label: 'fa-database', value: 'fa-database' },
    { label: 'fa-address-book-o', value: 'fa-address-book-o' }
  );
  /** 请求方式 */
  requestTypes: SelectItem[] = new Array(
    { label: '自定义', value: 0 },
    { label: 'Ajax请求', value: 1 },
    { label: '文件下载', value: 2 },
    { label: '打开窗口', value: 3 },
  );
  /** 响应方式 */
  responseTypes: SelectItem[] = new Array(
    { label: '自定义', value: 0 },
    { label: '刷新列表', value: 1 }
  );
  constructor(
    private fb: FormBuilder,
    private dialogService: DialogService,
    private taskTabService: TaskTabService,
    private confirmationService: ConfirmationService,
    private mdlSnackbarService: MdlSnackbarService,
  ) { }

  ngOnInit() {
    this.loadTaskListTabTree();
  }

  /**加载操作项 */
  loadTaskListTabTree() {
    this.loading = true;
    this.taskTabService.listTabTree(this.systemUrl).subscribe(tabs => {
      this.loading = false;
      this.tabs = tabs;
      if (this.tabs.length > 0 && this.tabs[0].children) {
        this.selectedNode = this.tabs[0].children[0];
        this.nodeSelect();
      }
    });
  }
  /**保存或更新菜单分组 */
  editGroup(optFlag) {
    let params = {};
    if ('edit' == optFlag) {
      params['groupId'] = this.selectedNode['id'].replace('grp_', '') || '';
      params['groupName'] = this.selectedNode.label || '';
    }
    const ref = this.dialogService.open(TaskTabGroupComponent, {
      'header': 'edit' == optFlag ? '编辑分组' : '新增分组',
      'width': '400px',
      contentStyle: { "height": "200px" },
      data: params
    });

    ref.onClose.subscribe((data: any) => data && this.loadTaskListTabTree());
  }

  /**删除分组 */
  deleteGroup() {
    this.confirmationService.confirm({
      message: '您确定要删除选择的分组吗?',
      header: '确认',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.taskTabService.deleteGroup(this.selectedNode['id'].replace('grp_', ''), this.systemUrl).subscribe(data => {
          this.mdlSnackbarService.showToast("删除分组成功");
          this.loadTaskListTabTree();
        });
      }
    });
  }

  buildFrom() {
    this.tabForm = this.fb.group({
      'tabId': [this.tab.tabId],
      'tabName': [this.tab.tabName],
      'tabIcon': [this.tab.tabIcon],
      'defaultAuth': [this.tab.defaultAuth],
      'componentName': [this.tab.componentName],
      'displayOrder': [this.tab.displayOrder],
      'tabDesc': [this.tab.tabDesc]
    });
  }

  nodeSelect() {
    if (!this.selectedNode) return;
    if (this.selectedNode['rootFlag']) {
      this.currentGroupId = this.selectedNode['id'].replace('grp_', '');
    } else {
      this.taskTabService.load(this.selectedNode['id']).subscribe(tab => {
        this.tab = tab;
        this.currentGroupId = this.tab.groupId;
        this.buildFrom();
      });
    }
  }

  /** 新增操作项 */
  addTab() {
    this.tab = {};
    this.buildFrom();
  }

  /** 保存操作项 */
  saveTab() {
    let value = this.tabForm.value;
    value.groupId = this.currentGroupId;
    this.taskTabService.saveUpdate(value, this.systemUrl).subscribe(data => {
      this.mdlSnackbarService.showToast("菜单保存成功");
      this.loadTaskListTabTree();
    });
  }

  /** 删除操作项 */
  deleteTab() {
    this.confirmationService.confirm({
      message: '您确定要删除选择的分组吗?',
      header: '确认',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.taskTabService.delete(this.tab.tabId, this.systemUrl).subscribe(data => {
          this.mdlSnackbarService.showToast("菜单删除成功");
          this.loadTaskListTabTree();
        });
      }
    });
  }
}
